package com.java.example.ui.dialog

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.SPUtils
import com.java.example.R
import com.java.example.app.Constant

class MeasureTypeDialog private constructor(private val mContext: Context) : View.OnClickListener {
    private var listener: OnActionListener? = null
    private var mDialog: AlertDialog? = null

    fun showDialog(context: Context?, tabone: String?, tabtwo: String?, tabthr: String?, luo: Int, allScreen: Int, isOnline: Boolean) {
        mDialog = AlertDialog.Builder(context, R.style.normal_dialog).create()
        mDialog!!.setCancelable(false)
        mDialog!!.setCanceledOnTouchOutside(true)
        mDialog!!.show()
        mDialog!!.window?.let {
            it.setContentView(R.layout.dialog_measure_type_choose)
            it.setGravity(Gravity.BOTTOM)
            it.setWindowAnimations(R.style.anim_panel_up_from_bottom)
            it.decorView.setPadding(0, 0, 0, 0)
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置属性
            val params = it.attributes
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.dimAmount = 0.5f
            it.attributes = params
            val textView1 = it.findViewById<TextView>(R.id.btn_measure_type_1)
            val textView2 = it.findViewById<TextView>(R.id.btn_measure_type_3)
            val textView4 = it.findViewById<TextView>(R.id.btn_measure_type_4)
            val llFast = it.findViewById<LinearLayout>(R.id.ll_fast)
            val llAllscreen = it.findViewById<LinearLayout>(R.id.ll_allscreen)
            val view0 = it.findViewById<View>(R.id.view0)
            val view1 = it.findViewById<View>(R.id.view1)
            //裸测按钮是否展示。旺财全部去掉
            if (luo == 1) {
//                llFast.visibility = View.VISIBLE
//                view0.visibility = View.VISIBLE
            }
            //全景按钮是否展示
            if (allScreen == 1) {
                llAllscreen.visibility = View.VISIBLE
                view1.visibility = View.VISIBLE
            }

            textView1.text = tabtwo
            textView2.text = tabone
            textView4.text = tabthr
            it.findViewById<View>(R.id.ll_fast).setOnClickListener(this)
            it.findViewById<View>(R.id.ll_mark).setOnClickListener(this)
            it.findViewById<View>(R.id.ll_allscreen).setOnClickListener(this)
            it.findViewById<View>(R.id.btn_cancel).setOnClickListener(this)
            it.findViewById<View>(R.id.ll_picture).setOnClickListener(this)

            val btn = it.findViewById<LinearLayout>(R.id.ll_mode_measure)
            val view = it.findViewById<View>(R.id.view3)
            if (SPUtils.getInstance().getBoolean(Constant.IS_HAOYUNLAI) && isOnline) {
                view.visibility = View.VISIBLE
                btn.visibility = View.VISIBLE
                btn.setOnClickListener(this)
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_cancel -> mDialog!!.dismiss()
            R.id.btn_measure_type_1, R.id.ll_mark -> {
                if (listener != null) {
                    listener!!.onChooseType(111)
                }
                mDialog!!.dismiss()
            }

            R.id.btn_measure_type_2 -> {
                if (listener != null) {
                    listener!!.onChooseType(112)
                }
                mDialog!!.dismiss()
            }

            R.id.btn_measure_type_3, R.id.ll_fast -> {
                if (listener != null) {
                    listener!!.onChooseType(113)
                }
                mDialog!!.dismiss()
            }

            R.id.ll_picture -> {
                if (listener != null) {
                    listener!!.onChooseType(115)
                }
                mDialog!!.dismiss()
            }

            R.id.ll_allscreen -> {
                if (listener != null) {
                    listener!!.onChooseType(114)
                }
                mDialog!!.dismiss()
            }

            R.id.ll_mode_measure -> {
                if (listener != null) {
                    listener!!.onChooseType(117)
                }
                mDialog!!.dismiss()
            }
        }
    }

    interface OnActionListener {
        fun onChooseType(type: Int)
    }

    fun setOnActionListener(listener: OnActionListener?) {
        this.listener = listener
    }

    companion object {
        fun create(mContext: Context): MeasureTypeDialog {
            return MeasureTypeDialog(mContext)
        }
    }
}
